﻿#include "RedMoonEvil.h"
#include "../../../Shared/Data/Stat.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../../Settings.h"
#include "../MapObject.h"
#include "../DelayedAction.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    bool RedMoonEvil::getCanMove() const
    {
        return false;
    }

    bool RedMoonEvil::getCanRegen() const
    {
        return false;
    }

    RedMoonEvil::RedMoonEvil(MonsterInfo *info) : MonsterObject(info)
    {
        setDirection(MirDirection::Up);

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
    }

    bool RedMoonEvil::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }

        return getTarget()->getCurrentLocation() != getCurrentLocation() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), Info->ViewRange);
    }

    void RedMoonEvil::Turn(MirDirection dir)
    {
    }

    bool RedMoonEvil::Walk(MirDirection dir)
    {
        return false;
    }

    int RedMoonEvil::Attacked(MonsterObject *attacker, int damage, DefenceType type)
    {
        int armour = 0;

        switch (type)
        {
            case DefenceType::ACAgility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                armour = GetAttackPower(Stats[Stat::MinAC], Stats[Stat::MaxAC]);
                break;
            case DefenceType::AC:
                armour = GetAttackPower(Stats[Stat::MinAC], Stats[Stat::MaxAC]);
                break;
            case DefenceType::MACAgility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                armour = GetAttackPower(Stats[Stat::MinMAC], Stats[Stat::MaxMAC]);
                break;
            case DefenceType::MAC:
                armour = GetAttackPower(Stats[Stat::MinMAC], Stats[Stat::MaxMAC]);
                break;
            case DefenceType::Agility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                break;
        }

        if (armour >= damage)
        {
            return 0;
        }

        ShockTime = 0;

        for (int i = PoisonList.size() - 1; i >= 0; i--)
        {
            if (PoisonList[i]->PType != PoisonType::LRParalysis)
            {
                continue;
            }

            PoisonList.erase(PoisonList.begin() + i);
            OperateTime = 0;
        }

        if (attacker->Info->AI == 6)
        {
            EXPOwner = nullptr;
        }
        else if (attacker->Master != nullptr)
        {
            if (EXPOwner == nullptr || EXPOwner->Dead)
            {
                EXPOwner = attacker->Master;
            }

            if (EXPOwner == attacker->Master)
            {
                EXPOwnerTime = getEnvir()->getTime() + EXPOwnerDelay;
            }
        }

        S::ObjectStruck *tempVar = new S::ObjectStruck();
        tempVar->ObjectID = ObjectID;
        tempVar->AttackerID = attacker->ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        Broadcast(tempVar);

        ChangeHP(-1);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        return 1;
    }

    int RedMoonEvil::Attacked(PlayerObject *attacker, int damage, DefenceType type, bool damageWeapon)
    {
        int armour = 0;

        switch (type)
        {
            case DefenceType::ACAgility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                armour = GetAttackPower(Stats[Stat::MinAC], Stats[Stat::MaxAC]);
                break;
            case DefenceType::AC:
                armour = GetAttackPower(Stats[Stat::MinAC], Stats[Stat::MaxAC]);
                break;
            case DefenceType::MACAgility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                armour = GetAttackPower(Stats[Stat::MinMAC], Stats[Stat::MaxMAC]);
                break;
            case DefenceType::MAC:
                armour = GetAttackPower(Stats[Stat::MinMAC], Stats[Stat::MaxMAC]);
                break;
            case DefenceType::Agility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                break;
        }

        if (armour >= damage)
        {
            return 0;
        }

        if (damageWeapon)
        {
            attacker->DamageWeapon();
        }

        ShockTime = 0;

        for (int i = PoisonList.size() - 1; i >= 0; i--)
        {
            if (PoisonList[i]->PType != PoisonType::LRParalysis)
            {
                continue;
            }

            PoisonList.erase(PoisonList.begin() + i);
            OperateTime = 0;
        }

        if (Master != nullptr && Master != attacker)
        {
            if (getEnvir()->getTime() > Master->BrownTime && Master->getPKPoints() < 200)
            {
                attacker->BrownTime = getEnvir()->getTime() + Settings::Minute;
            }
        }

        if (EXPOwner == nullptr || EXPOwner->Dead)
        {
            EXPOwner = attacker;
        }

        if (EXPOwner == attacker)
        {
            EXPOwnerTime = getEnvir()->getTime() + EXPOwnerDelay;
        }

        S::ObjectStruck *tempVar = new S::ObjectStruck();
        tempVar->ObjectID = ObjectID;
        tempVar->AttackerID = attacker->ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        Broadcast(tempVar);
        attacker->GatherElement();
        ChangeHP(-1);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        return 1;
    }

    void RedMoonEvil::ProcessTarget()
    {
        if (!getCanAttack())
        {
            return;
        }

        std::vector<MapObject*> targets = FindAllTargets(Info->ViewRange, getCurrentLocation());
        if (targets.empty())
        {
            return;
        }

        ShockTime = 0;

        S::ObjectAttack *tempVar = new S::ObjectAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        Broadcast(tempVar);
        for (int i = 0; i < targets.size(); i++)
        {
            setTarget(targets[i]);
            Attack();
        }


        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void RedMoonEvil::Attack()
    {
        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }

        DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::ACAgility});
        ActionList.push_back(action);

        S::ObjectEffect *tempVar = new S::ObjectEffect();
        tempVar->ObjectID = getTarget()->ObjectID;
        tempVar->Effect = SpellEffect::RedMoonEvil;
        Broadcast(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
    }
}
